package com.ytjj.qmyx.mall.controller.h5;

import com.ytjj.common.api.CommonResult;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.enums.ActivityTypeEnums;
import com.ytjj.qmyx.mall.model.dto.OldForNewProductDto;
import com.ytjj.qmyx.mall.model.response.ActivityProductResponse;
import com.ytjj.qmyx.mall.service.ActivityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 会员享补贴活动
 *
 **/
@RestController
@RequestMapping("/h5/membersubsidy")
public class H5MemberSubsidyController extends BaseController {
    @Resource
    private ActivityService activityService;

    @Resource
    private RedisService redisService;
    /**
     * 栏目商品列表
     *
     * @return
     */
    @GetMapping("/queryListByPage")
    public CommonResult queryListByPage(@RequestParam(value = "columnId", required = false, defaultValue = "0") Integer columnId,
                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag) {

        List<OldForNewProductDto> productList = null;
        List<ActivityProductResponse> activityProductList = activityService.getActivityList(pageNum, pageSize,ActivityTypeEnums.MEMBER_SUBSIDY.name(),columnId);
        if(1==flag){//打乱顺序
            Collections.shuffle(activityProductList);
        }
        String subsidyKey = com.ytjj.qmyx.mall.constants.RedisConstants.SUBSIDYKEY;
        String token = TokenUtil.getToken();

        productList = activityProductList.stream()
                .map(item -> {
                    OldForNewProductDto oldForNewProductDto = new OldForNewProductDto();
                    BeanUtils.copyProperties(item,oldForNewProductDto);
                    Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                    if (subsidyHash != null && subsidyHash.size() > 0) {
                        Object subsidyData = subsidyHash.get(item.getId().toString());
                        if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                            oldForNewProductDto.setIsReceiveSubsidy(1);
                        }else {
                            oldForNewProductDto.setIsReceiveSubsidy(0);
                        }
                    }
                    return oldForNewProductDto;
                })
                .collect(Collectors.toList());
        return CommonResult.success(productList);
    }
}
